<?php
require_once "weixin_conf.php";
$scope = 'snsapi_userinfo';
$state = 'dorabox_weixin_ih_web_auth';
ob_end_clean();



if ( isset($_GET["duolastate"]) && $_GET["duolastate"] == $state) {
    if (empty($_GET['duolacode'])) {
        echo "授权失败";
        exit;
    } else {
        $code = $_GET['duolacode'];
        $web_token_data = getWeixin_WebAccess_Token($code,WEIXIN_APPID,WEIXIN_AppSECRET);
        $re=get_weixin_userinfo($web_token_data);
//        var_dump($re);exit;
        return ['openid'=>$re->openid,'nickname'=>$re->nickname,'headimgurl'=>$re->headimgurl];
    }
} else {
       $REDIRECT_URI = curPageURL();
//    echo $REDIRECT_URI;exit;
       $appid = WEIXIN_APPID;
       $url = "http://wx.duolahe.com/biz_weixin/web/get-weixin-code_default.html?appid=$appid&scope=$scope&state=$state&redirect_uri=" . urlencode($REDIRECT_URI);
       header("Location: $url");
       exit;

}

// 说明：获取完整URL

function curPageURL()
{
    $pageURL = 'http';

    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";

    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}


function getWeixin_WebAccess_Token($CODE,$appid,$AppSecret)
{

    $re = weixin_curl("https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$AppSecret&code=$CODE&grant_type=authorization_code");
    $re = json_decode($re);
    //{"errcode":40029,"errmsg":"invalid code"}
    if (isset($re->errcode)) {
        echo '<h1>错误：</h1>' . $re->errcode;
        echo '<br/><h2>错误信息：</h2>' . $re->errmsg;
        die;
    } else {
        return $re;
    }
}
//    获取微信用户信息
 function get_weixin_userinfo($web_token_data){
    $url="https://api.weixin.qq.com/sns/userinfo?access_token=$web_token_data->access_token&openid=$web_token_data->openid&lang=zh_CN";
    $re =  weixin_curl($url);
    return json_decode($re);
}
function weixin_curl($url, $postFields = NULL)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //https 请求
    if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == 'https') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }

    if (is_array($postFields) && 0 < count($postFields)) {
        $postBodyString = '';
        $postMultipart = FALSE;
        foreach ($postFields as $k => $v) {
            if ('@' != substr($v, 0, 1)) //判断是不是文件上传
            {
                $postBodyString .= "$k=" . urlencode($v) . "&";
            } else {
                //文件上传用multipart/form-data，否则用www-form-urlencoded
                $postMultipart = TRUE;
            }
        }
        $postFields = trim($postBodyString, '&');
        unset($k, $v);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        if ($postMultipart) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        }
    }

    $reponse = curl_exec($ch);
    curl_close($ch);
    return $reponse;
}